package org.example.elastic.lite.jobs;

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.dataflow.job.DataflowJob;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * 数据流作业
 * @author 魏荣杰
 * @date 2022/11/11 09:54
 * @since
 * @version
 */
@Slf4j
@Component
public class ExampleDataflowJob implements DataflowJob<Integer> {

    @Override
    public List<Integer> fetchData(ShardingContext shardingContext) {
        log.info("ExampleDataflowJob fetchData start");
        String jobName = shardingContext.getJobName();
        String jobParameter = shardingContext.getJobParameter();
        int shardingItem = shardingContext.getShardingItem();
        String shardingParameter = shardingContext.getShardingParameter();
        int shardingTotalCount = shardingContext.getShardingTotalCount();
        String taskId = shardingContext.getTaskId();
        log.info("jobName: {}, jobParameter: {}, shardingItem: {}, shardingParameter: {}, shardingTotalCount: {}, taskId: {}", jobName, jobParameter, shardingItem, shardingParameter, shardingTotalCount, taskId);
        log.info("ExampleDataflowJob fetchData end");
        return Arrays.asList(1,2,3);
    }

    @Override
    public void processData(ShardingContext shardingContext, List<Integer> list) {
        log.info("ExampleDataflowJob processData start");
        log.info("list: {}", list);
        String jobName = shardingContext.getJobName();
        String jobParameter = shardingContext.getJobParameter();
        int shardingItem = shardingContext.getShardingItem();
        String shardingParameter = shardingContext.getShardingParameter();
        int shardingTotalCount = shardingContext.getShardingTotalCount();
        String taskId = shardingContext.getTaskId();
        log.info("jobName: {}, jobParameter: {}, shardingItem: {}, shardingParameter: {}, shardingTotalCount: {}, taskId: {}", jobName, jobParameter, shardingItem, shardingParameter, shardingTotalCount, taskId);
        log.info("ExampleDataflowJob processData end");
    }

}
